I have the following code:
import javax.swing.*
...
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName())
UIManager.put("ToolTip.border", BorderFactory.createEmptyBorder())
UIManager.put("PopupMenu.border", BorderFactory.createEmptyBorder())
...
I want to get rid of all UIManager. qualifiers, like this:
UIManager.run {
setLookAndFeel(getCrossPlatformLookAndFeelClassName())
put("ToolTip.border", BorderFactory.createEmptyBorder())
put("PopupMenu.border", BorderFactory.createEmptyBorder())
...
}
Of course, this code doesn't compile. Is it possible to achieve?
you can use 'with' keyword to achieve that. This is one of the scoping functions in Kotlin.
with(UIManager)
{
setLookAndFeel(getCrossPlatformLookAndFeelClassName())
put("ToolTip.border", BorderFactory.createEmptyBorder())
put("PopupMenu.border", BorderFactory.createEmptyBorder())
...
}
Ultimately you are expecting some sort of scoping function.This article gives detailed explanation about usecase for each scoping function in Kotlin. https://medium.com/@elye.project/mastering-kotlin-standard-functions-run-with-let-also-and-apply-9cd334b0ef84